package com.gouwudai.gobbs.dao.impl;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.gouwudai.gobbs.common.IPageList;
import com.gouwudai.gobbs.dao.IMasterDAO;
import com.gouwudai.gobbs.pojo.TbbsMaster;
import com.litt.core.common.PageParam;
import com.litt.core.common.Utility;
import com.litt.core.exception.BusiException;
import com.litt.core.exception.DBException;

public class MasterDAOImpl extends BaseDAOImpl implements IMasterDAO 
{
	private static final Logger logger = Logger.getLogger(MasterDAOImpl.class);	
	private static final String BUSI_NAME = "管理员信息";
	
	public void save(TbbsMaster tbbsMaster) throws BusiException
	{
    	if(this.load(tbbsMaster.getLoginId())!=null)
    		throw new DBException("该登陆ID已存在！");
    	if(this.loadByName(tbbsMaster.getMasterName())!=null)
    		throw new DBException("该管理员名称已存在！");	
		//加密密码
		String md5Pass = Utility.MD5EncodeString(tbbsMaster.getPassword());
		tbbsMaster.setPassword(md5Pass);
		super.saveObject(tbbsMaster);		
	}
	
	public TbbsMaster load(Integer id)
	{
		return (TbbsMaster)super.loadObject(TbbsMaster.class, id);
	} 

	
	/**
	 * 根据登陆名获得用户信息
	 * @param loginId
	 * @return
	 */
	public TbbsMaster load(String loginId) 
	{		
		String hql = "from TbbsMaster where loginId='"+loginId+"'";	
		TbbsMaster masterInfo = (TbbsMaster)super.uniqueResultHql(hql);
		
		return masterInfo;
	}
	
	/**
	 * 根据用户名获得用户信息
	 * @param loginId
	 * @return
	 */
	public TbbsMaster loadByName(String masterName) 
	{		
		String hql = "from TbbsMaster where masterName='"+masterName+"'";	
		TbbsMaster masterInfo = (TbbsMaster)super.uniqueResultHql(hql);
		
		return masterInfo;
	}	
    
    public void update(TbbsMaster tbbsMaster) throws BusiException
    {
    	//验证 	
		String vhql = "from TbbsMaster where masterName='"+tbbsMaster.getMasterName()+"' and masterId<>"+tbbsMaster.getMasterId();	
		TbbsMaster masterInfo = (TbbsMaster)super.uniqueResultHql(vhql);
		if(masterInfo!=null)
			throw new DBException("该管理员名称已存在！");	
		//验证结束
		//---
    	StringBuilder hql = new StringBuilder();
        hql.append("update TbbsMaster set masterName=?");
        hql.append(", ipLockFlag=?");
        hql.append(", lockBeginIp=?");
        hql.append(", lockEndIp=?");
        hql.append(", mgId=?");
        hql.append(" where masterId=?");
        super.updateObject(hql.toString(), new Object[]{
        	tbbsMaster.getMasterName(),
        	tbbsMaster.getIpLockFlag(),
        	tbbsMaster.getLockBeginIp(),
        	tbbsMaster.getLockEndIp(),
        	tbbsMaster.getMgId(),
        	tbbsMaster.getMasterId()});

    }
    
    /**
     * 重置密码
     * @param password
     * @param masterId
     * @throws DBException
     */
    public void updatePassword(String password,Integer masterId)
    {
		//加密密码		
		String md5Pass = Utility.MD5EncodeString(password);	
		//---
        String hql = "update TbbsMaster set password=? where masterId=?";
        super.updateObject(hql, new Object[]{      
        	md5Pass,
        	masterId});

    }    
    
    /**
     * 管理员登陆时更新相关信息
     * @param tbbsMaster
     * @throws DBException
     */
    public void doLoginUpdate(Integer masterId,String loginIp)
    {
    	StringBuilder hql = new StringBuilder();
        hql.append("update TbbsMaster set lastLoginTime=?");
        hql.append(", lastLoginIp=?");
        hql.append(" where masterId=?");
        super.updateObject(hql.toString(), new Object[]{        	
        	new Date(),
        	loginIp,
        	masterId});
    }    
    

    public void delete(Integer masterId)
    {
        super.deleteObject(TbbsMaster.class.getName(),"masterId",masterId);
    }

    public IPageList listPage(PageParam pageParam)
    {       
    	StringBuffer listSql = new StringBuffer("SELECT A.*,B.GRADE_NAME FROM TbbsMaster A,TbbsMasterGrade B WHERE A.MG_ID=B.MG_ID ");
        if(pageParam.hasSort())
            listSql.append(pageParam.getSort());  
    	return super.listSqlPage(listSql.toString(),pageParam.getPageIndex(),pageParam.getPageSize());
    }

    
    public List getMgPermission(Integer mgId)
    {
    	String hql = "select a from TbbsMasterPermission a,TbbsMasterGradePermission b where b.mgId="+mgId+" and b.permissionCode=a.permissionCode";
    	return super.listAll(hql);
    }
    
}
